import java.util.ArrayList;

//两个数组的交集II
public class Solution2 {
    public int[] intersect(int[] nums1, int[] nums2) {
        int[] hash1 = new int[1001];
        int[] hash2 = new int[1001];
        ArrayList<Integer> result = new ArrayList<>();
        for(int i = 0; i < nums1.length || i < nums2.length ; i++){
            if(i < nums1.length){
                hash1[nums1[i]]++;
            }
            if(i < nums2.length) {
                hash2[nums2[i]]++;
            }
        }
        for (int i = 0; i < 1001; i++) {
            if(hash1[i] > 0 && hash2[i] > 0){
                int min = Math.min(hash1[i], hash2[i]);
                while(min-- != 0){
                    result.add(i);
                }
            }
        }
        int[] ret = new int[result.size()];
        int k = 0;
        for (Integer integer : result) {
            ret[k++] = integer;
        }
        return ret;
    }
}
